package com.kcl.dfss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.http.CameraConnection.ResponseHandler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SaveRecordActivity extends Activity {

	protected static final String tag = "SaveRecordActivity";
	private List<Map<String, String>> mRecordList = null;
	private LinearLayout vh_back;
	private ListView video_list;
	private TextView video_remind;
	private Handler handler = null;
	private SimpleAdapter adapter  = null;
	private boolean isconnect = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_save_record);
		
		vh_back = (LinearLayout) findViewById(R.id.vh_back);
		video_list = (ListView) findViewById(R.id.video_list);
		video_remind = (TextView) findViewById(R.id.video_remind);
		
		vh_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		getRecordsFromCamera();
		isconnect = LocationService.getCamemraConnected();
		if(!isconnect){
			video_list.setVisibility(View.GONE);
			video_remind.setVisibility(View.VISIBLE);
			video_remind.setText("查看行驶记录请先连接设备");
		}
		
		
		handler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 1){
					if(mRecordList.size() == 0){
						video_list.setVisibility(View.GONE);
						video_remind.setVisibility(View.VISIBLE);
					}else{
						video_list.setVisibility(View.VISIBLE);
						video_remind.setVisibility(View.GONE);
						adapter = new SimpleAdapter(SaveRecordActivity.this, mRecordList, R.layout.device_item4,
								new String[]{"history"}, new int[]{R.id.text_historydevice});
						video_list.setAdapter(adapter);
						video_list.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {
								String url = mRecordList.get(position).get("history");
								Intent intent = new Intent();
								intent.setClass(getBaseContext(), VideoPlayerActivity.class);
								intent.putExtra("URL", url);
								startActivity(intent);
							}
						});
					}
				}
			}
		};
		
	}
	
	public void getRecordsFromCamera() {
		if (LocationService.getCamemraConnected()) {
			CameraConnection mConnection = new CameraConnection(
					new ResponseHandler() {
						@Override
						public void handleResponse(int request_code,
								String response) {
							// TODO Auto-generated method stub
							if (request_code == CameraConnection.CODE_GET_FILELIST) {
								Log.i(tag, "返回未处理："+response);
								getRecordsFromResponse(response);
								Message message = new Message();
								message.what = 1;
								handler.sendMessage(message);
							}
						}
					}, this.getBaseContext());
			
			mConnection.getFilelist();
		}
	}
	
	/*public void getRecordsFromResponse(String string) {
		String[] list = string.split("\n");
		mRecordList = new ArrayList<Map<String,String>>();
		for(int i=0;i<list.length;i++){
			Map<String, String> map = new HashMap<String, String>();
			map.put("history", list[i]);
			mRecordList.add(map);
		}
		Log.i(tag, "返回未处理："+mRecordList.toString());
	}*/
	
	public void getRecordsFromResponse(String string){
		String[] list = string.split(".mkv");
		mRecordList = new ArrayList<Map<String,String>>();
		for (int i = 0; i < list.length; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("history", list[i]);
			mRecordList.add(map);
		}
	}

}
